#!/bin/bash
# written by Kamil Cukrowski

BINARY=./qsearch
kids=""
kids_num=0
max_kids_num=300
all_kids=0
TEMPFILE=/tmp/qwerty

echo > $TEMPFILE

function wait_for_one_kid_to_die()
{
	while true; do
		for PID in $kids; do
			if test ! -f /proc/$PID/exe; then
				kids=$(echo "$kids" | sed 's/'$PID'//' )
				kids_num=$[$kids_num-1]
				return;
			fi
		done
	done
}

list=$(mysql -u serwus --password=serwus123 qsearch -e "select name from hosts" | tail -n +2)
for ip in $list; do
	ip="10.$ip"
	name=$( host $ip | awk '{print $5}' | sed 's/\.pw\.edu\.pl\.//' ) 

#	( exec $BINARY $i $ip ) & 
#	kids=( ${kids[@]} $! )
	
	echo $name

#	( exec $BINARY $name $ip 2>/dev/null >/dev/null ) &
#	kids="$! $kids"
#	kids_num=$[$kids_num+1]
#	all_kids=$[$all_kids+1]
#	
#	echo  $kids_num -all_kids $all_kids -- bla $ip -- bla $name --- - kids $kids -kids_num
#	
#	if test $max_kids_num -le $kids_num; then
#		wait_for_one_kid_to_die
#	fi
	
 
done

#for daemon in ${kids[@]}; do
#	wait $daemon
#done

